#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/9/11 3:36
# @Author  : leon yan
# @Site    : 
# @File    : qTableWidgetObj.py
# @Software: PyCharm

from PyQt5.QtCore import QObject, Qt, QPoint
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QCheckBox, QMenu, QTableWidgetItem, QMessageBox

import lib.qsqledit as QSql


class QTableWidgetObj(QObject):
    def moveRow(self, tableobj, curRow, toRow):
        if not tableobj:
            return
        rowCount = tableobj.rowCount()
        columCount = tableobj.columnCount()

        if curRow == toRow:
            return
        if curRow < 0 or toRow < 0 or curRow > rowCount or toRow > rowCount:
            return

        if toRow < curRow:
            curRow += 1
            cursorRow = toRow
        else:
            cursorRow = toRow
            toRow += 1
        tableobj.insertRow(toRow)
        for i in range(columCount):
            if i == 0:
                chkBox = QCheckBox(tableobj)
                h = QHBoxLayout()
                h.setAlignment(Qt.AlignCenter)
                h.addWidget(chkBox)
                w = QWidget()
                w.setLayout(h)
                tableobj.setCellWidget(toRow, 0, w)
            if i > 0:
                cellItem = tableobj.item(curRow, i)
                item = QTableWidgetItem(cellItem.text(), 1000)
                item.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                font = item.font()
                font.setPointSize(8)
                item.setFont(font)
                item.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)  # 不允许编辑文字
                tableobj.setItem(toRow, i, item)
        tableobj.removeRow(curRow)
        tableobj.selectRow(cursorRow)

    '''列表右键弹出菜单-0'''

    def generateEditMenu(self, pos, tableobj):
        pos += QPoint(0, 30)
        row_num = -1
        for i in tableobj.selectionModel().selection().indexes():
            row_num = i.row()

        if row_num >= 0:
            menu = QMenu()
            item1 = menu.addAction(u"编辑")
            action = menu.exec_(tableobj.mapToGlobal(pos))
            if action == item1:
                return "Edit"
            else:
                return

    '''列表右键弹出菜单-1'''

    def generateMenu(self, pos, tableobj):
        pos += QPoint(0, 30)
        row_num = -1
        for i in tableobj.selectionModel().selection().indexes():
            row_num = i.row()

        if row_num > 0:
            menu = QMenu()
            item1 = menu.addAction(u"上移")
            item2 = menu.addAction(u"下移")
            action = menu.exec_(tableobj.mapToGlobal(pos))
            if action == item1:
                return "up"
            elif action == item2:
                return "down"
            else:
                return

    '''遍历列表，第一列为复选框时的情况'''

    def readListtable(self, tableobj):
        pass
        '''复选框'''
        chkBox = QCheckBox(tableobj)
        h = QHBoxLayout()
        h.setAlignment(Qt.AlignCenter)
        h.addWidget(chkBox)
        w = QWidget()
        w.setLayout(h)
        ''''''
        rowCount = tableobj.rowCount()
        colCount = tableobj.columnCount()
        strtabList = []
        for i in range(rowCount):
            strrowList = []
            for j in range(colCount):
                cellItem = tableobj.item(i, j)
                if j == 0:
                    strrowList.append(chkBox)
                else:
                    strrowList.append(cellItem.text())
            strtabList.append(strrowList)
        return strtabList

    '''删除选择行(包含表对应的数据库列表)'''

    def delSelectTabline(self, tableLineobj, tableobj, sqlTableName):
        removeline = []
        for line in tableLineobj:
            if line[0].isChecked():
                row = tableobj.rowCount()
                for x in range(row, 0, -1):
                    if str(line[1]) == tableobj.item(x - 1, 1).text():
                        tableobj.removeRow(x - 1)
                        QSql.DeleteDbRecord(sqlTableName, int(line[1]))
                        removeline.append(line)
        # print(removeline)
        # print(tableLineobj)
        for line in removeline:
            tableLineobj.remove(line)
        # print(tableLineobj)

    '''删除全部行(包含表对应的列表)'''

    def delAllTabline(self, tableLineobj, tableobj):
        pass
        '''删除确认?'''
        ret = QMessageBox.question(None, "删除确认", "确定要清除列表吗？")
        if (ret != QMessageBox.Yes):
            return
        if (ret == QMessageBox.Yes):
            rowCount = tableobj.rowCount()  # 表格行数
            for x in range(rowCount, 0, -1):
                tableobj.removeRow(x - 1)
                if tableLineobj:
                    tableLineobj.pop()

    '''删除全部行(包含表对应的列表)'''
    '''无提示删除'''

    def delAllTablineM2(self, tableLineobj, tableobj):
        rowCount = tableobj.rowCount()  # 表格行数
        for x in range(rowCount, 0, -1):
            tableobj.removeRow(x - 1)
            if tableLineobj:
                tableLineobj.pop()
